//
//  TIMMyselfViewController.m
//  Demo_sdk
//
//  Created by Deli Xia on 2018/6/28.
//  Copyright © 2018年 Deli Xia. All rights reserved.
//

#import "TIMMyselfViewController.h"
#import "TIMDemoMacro.h"
#import "TIMLoginViewController.h"

#import "TIMStorageModel.h"
#import "TIMStorageManager.h"
#import "TIMApi.h"
@interface TIMMyselfViewController ()

@property (nonatomic, strong) UIView *headView;
@property (nonatomic,strong)UIImageView *icon;
@property (nonatomic,strong)UILabel *nikeNameLB;
@property (nonatomic,strong)UILabel *userIDLB;

@property (nonatomic, strong) UIButton *logoutBtn;

@property (nonatomic, strong) TIMUserInfo *user;
@end

@implementation TIMMyselfViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self setUpNav];
    [self.view addSubview:self.headView];
    [self.headView addSubview:self.icon];
    [self.headView addSubview:self.nikeNameLB];
    [self.headView addSubview:self.userIDLB];
    self.navigationItem.title = @"我的";
    [self.view addSubview:self.logoutBtn];
}

- (void)setUpNav {
    
    self.view.backgroundColor = UIColor.whiteColor;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 65, 25);
    btn.titleLabel.font = TPSFontSize(15);
    [btn setTitle:@"编辑资料" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(editClicked) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}

- (void)editClicked {
    
}

- (void)clickLogout {
    [TIMApi logout:^(NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            TIMLoginViewController *main = [[TIMLoginViewController alloc] init];
            [UIApplication sharedApplication].keyWindow.rootViewController = main;
        });
    }];
}

#pragma mark - Get

- (UIView *)headView {
    if (!_headView) {
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.center.y)];
        _headView.backgroundColor = RGBColor(221, 221, 221);
    }
    return _headView;
}

- (UIImageView *)icon {
    if (!_icon) {
        _icon = [UIImageView new];
        _icon.contentMode = UIViewContentModeScaleAspectFill;
        _icon.frame = Frame(self.headView.frame.size.width/2 - 70/2,100, 70,70);
        _icon.layer.cornerRadius = 70/2;
        _icon.layer.masksToBounds = YES;
        if (self.user.avatarURL) {
            NSLog(@"头像URL是:%@",self.user.avatarURL);
            _icon.image = kLOADIMAGE(@"userNorm_image");
        } else {
             _icon.image = kLOADIMAGE(@"userNorm_image");
        }
    }
    return _icon;
}

- (UILabel *)nikeNameLB {
    if (!_nikeNameLB) {
        _nikeNameLB = [[UILabel alloc] initWithFrame: Frame(20,CGRectGetMaxY(self.icon.frame) + 15,self.headView.frame.size.width - 40, 25)];
        _nikeNameLB.font = [UIFont boldSystemFontOfSize:18];
        _nikeNameLB.textAlignment = NSTextAlignmentCenter;
        _nikeNameLB.textColor = UIColor.blackColor;
        _nikeNameLB.text = [NSString stringWithFormat:@"昵称：%@",self.user.nikeName];
    }
    return _nikeNameLB;
}

- (UILabel *)userIDLB {
    if (!_userIDLB) {
        _userIDLB = [[UILabel alloc] initWithFrame:Frame(20,CGRectGetMaxY(self.nikeNameLB.frame) + 5,self.headView.frame.size.width - 40, 17)];
        _userIDLB.font = [UIFont boldSystemFontOfSize:12];
        _userIDLB.textAlignment = NSTextAlignmentCenter;
        _userIDLB.textColor = UIColor.blackColor;
        _userIDLB.text = [NSString stringWithFormat:@"userID:%@",self.user.userID ];
    }
    return _userIDLB;
}

- (UIButton *)logoutBtn {
    if (!_logoutBtn) {
        _logoutBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _logoutBtn.frame = Frame(self.view.center.x - 50, self.view.center.y + 100, 100, 40);
        _logoutBtn.backgroundColor = UIColor.redColor;
        [_logoutBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_logoutBtn setTitle:@"注销" forState:UIControlStateNormal];
        [_logoutBtn addTarget:self action:@selector(clickLogout) forControlEvents:UIControlEventTouchUpInside];
    }
    return _logoutBtn;
}

- (TIMUserInfo *)user {
    if (!_user) {
        _user = [[TIMStorageManager sharedStorageManager] getCurrentUserInfo];
    }
    return _user;
}

@end
